<? # -*- mode: shell-script -*-
# Copyright (C) 2009-2012 David Bird (Coova Technologies) <support@coova.com>
# 
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 2 of the License, or
# (at your option) any later version.
#  
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#  
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

. ./config.sh

if [ -n "$FORM_username" -a -n "$COOVA_CHALLENGE" ]
then
    http_header
    dologin
else
    
    if [ -e "splash.chi" ]
    then
	http_redirect "/www/splash.chi"
    fi
    
    http_header
    
    case "$FORM_res" in
	success|already)
	    url=$FORM_redirurl
	    url=${url:-$FORM_userurl}
	    header "<meta http-equiv=\"refresh\" content=\"5;url=${url}\"/>"
	    uamfile "login_success" 1
	    footer
	    exit
	    ;;
    esac
    
    header
    
    uamfile "login" 1 
    
    if [ "$FORM_res" =  "failed" ]
    then
	if [ -n "$FORM_reply" ]
	then
	    error "$FORM_reply"
	else
	    error "Username and/or password was not valid"
	fi
    fi
    
    loginform 
    
    uamfile "login_footer" 1 
    [ "$HS_REG_MODE" = "self" ] && uamfile "login_register" 1 
    [ "$HS_USE_MAP" = "on" ] && uamfile "login_map" 1 
    
    footer
    
fi

?>
